﻿using CollegeReviewMVC.Model;
using CollegeReviewMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CollegeReviewMVC.Controllers
{
    public class HomeController : Controller
    {
        
        public ActionResult Index()
        {
            List<College> model;
            using (ApplicationDbContext db = new ApplicationDbContext()) 
            {
                model = db.Colleges.Include("Reviews").ToList();
            }
            return View(model);
        }

        public ActionResult Details(int id) 
        {
            College model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
           
                model = db.Colleges.FirstOrDefault(x => x.CollegeId == id);
                
            }
                return View(model);
            }

        
        public ActionResult AddCollege() 
        {
           
            College model = new College();

            return View(model);
        }
        [HttpPost]
        public ActionResult AddCollege(College model) 
        {
            using (ApplicationDbContext db = new ApplicationDbContext()) 
            {
                
                db.Colleges.Add(model);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        public ActionResult EditCollege(int id) 
        {
            College Model;
            using (ApplicationDbContext db = new ApplicationDbContext()) 
            {
                  Model = db.Colleges.FirstOrDefault(x => x.CollegeId == id);
                
            }
            return View(Model);
        }
        [HttpPost]
        public ActionResult EditCollege(College editCollege) 
        {
            
            using (ApplicationDbContext db = new ApplicationDbContext()) 
            {
                College Model = db.Colleges.FirstOrDefault(x => x.CollegeId == editCollege.CollegeId);
                Model.CollegeName = editCollege.CollegeName;
                db.SaveChanges();  
            }
            return RedirectToAction("Index");
        }
        public ActionResult DeleteCollege(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College deleteCollege = db.Colleges.FirstOrDefault(x => x.CollegeId == id);
                db.Colleges.Remove(deleteCollege);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        public ActionResult AddReview() 
        {
            Review model = new Review();
            return View(model);
        }
        [HttpPost]
        public ActionResult AddReview(Review model) 
        {            
            using (ApplicationDbContext db = new ApplicationDbContext()) 
            {
                Review Model = db.Reviews.FirstOrDefault(x => x.CollegeId == model.CollegeId);
                db.Reviews.Add(model);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        public ActionResult EditReview(int id) 
        {
            Review Model;
            using (ApplicationDbContext db = new ApplicationDbContext()) 
            {
                Model = db.Reviews.FirstOrDefault(x => x.ReviewId == id);
            }
            return View(Model);
        }
        [HttpPost]
        public ActionResult EditReview(Review editReview) 
        {
            using (ApplicationDbContext db = new ApplicationDbContext()) 
            {
                Review Model = db.Reviews.FirstOrDefault(x => x.ReviewId == editReview.ReviewId);
                Model.ReviewPost = editReview.ReviewPost;
                db.SaveChanges();
            }
            return View(editReview);
        }
        public ActionResult DeleteReview(int id) 
        {
            using (ApplicationDbContext db = new ApplicationDbContext()) 
            {
                Review DeleteReview = db.Reviews.FirstOrDefault(x => x.ReviewId == id);
                db.Reviews.Remove(DeleteReview);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
   
    }
}